package com.yanqu.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DictMgr extends TempMgr {

    private static Map<Integer, String> wayNameMap = new ConcurrentHashMap<>();

    private static Map<Long, String> serverNameMap = new ConcurrentHashMap<>();

    private static Map<Integer, String> goodsNameMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        String center_url = ConfigHelper.getValue("center_url");
        if(!StringUtils.isNullOrEmpty(center_url)){

            String result =  HttpHelper.doPostJson(center_url + "/usercenter/dict/wayInfoList", "", "UTF-8");
            Map<Integer, String> wayNameMapTemp = JSON.parseObject(result, new TypeReference<Map<Integer, String>>(){});
            if(wayNameMapTemp != null){
                wayNameMap = wayNameMapTemp;
            }

            int channelId = 301;
            String channelIdValue = ConfigHelper.getValue("channelId");
            if(!StringUtils.isNullOrEmpty(channelIdValue)){
                channelId = Integer.parseInt(channelIdValue);
            }
            result =  HttpHelper.doPostJson(center_url + "/usercenter/dict/goodsInfoList?channelId=" + channelId, "", "UTF-8");
            Map<Integer, String> goodsNameMapTemp = JSON.parseObject(result, new TypeReference<Map<Integer, String>>(){});
            if(goodsNameMapTemp != null){
                goodsNameMap = goodsNameMapTemp;
            }

            result =  HttpHelper.doPostJson(center_url + "/usercenter/dict/serverInfoList", "", "UTF-8");
            Map<Long, String> serverNameMapTemp = JSON.parseObject(result, new TypeReference<Map<Long, String>>(){});
            if(serverNameMapTemp != null){
                serverNameMap = serverNameMapTemp;
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<Integer, String> getWayNameMap() {
        return wayNameMap;
    }

    public static String getWayName(int way){
        return wayNameMap.getOrDefault(way, "");
    }

    public static void setWayNameMap(Map<Integer, String> wayNameMap) {
        DictMgr.wayNameMap = wayNameMap;
    }

    public static Map<Long, String> getServerNameMap() {
        return serverNameMap;
    }

    public static String getServerName(long serverId){
        return serverNameMap.getOrDefault(serverId, "");
    }

    public static void setServerNameMap(Map<Long, String> serverNameMap) {
        DictMgr.serverNameMap = serverNameMap;
    }

    public static Map<Integer, String> getGoodsNameMap() {
        return goodsNameMap;
    }

    public static String getGoodsName(int goodsId){
        return goodsNameMap.getOrDefault(goodsId, "");
    }

    public static void setGoodsNameMap(Map<Integer, String> goodsNameMap) {
        DictMgr.goodsNameMap = goodsNameMap;
    }

}
